import { Destroyable, react, reactJson, Event, track, bind } from "xbsj-xe2/dist-node/xe2-base-utils";
import { ESHumanPoi } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';
export class PersonCollection extends Destroyable {
    _map = new Map();

    // 全局显隐控制(EarthSDK响应式属性)
    _show = this.disposeVar(react(false));
    get show() { return this._show.value; }
    set show(value) { this._show.value = value }
    get showChanged() { return this._show.changed; }

    setData(data) {
        if (Array.isArray(data) && data.length > 0) {
            data.forEach(item => {
                if (!this._map.has(item.id)) {
                    this.create(item)
                } else {
                    this.update(item)
                }
            })
        }
    }
    constructor(_myProjectManager) {
        super();
        this._myProjectManager = _myProjectManager
        this._sceneTree = _myProjectManager.createSceneTree("personSceneTree", 28)

        // 全局控制显隐属性，监听变化
        this.showChanged.don((val) => {
            this._map.forEach(item => {
                item.sceneObject.show = val
            })
        })
    }

    create(item) {
        const { id, name, position } = item
        const treeItem = this._sceneTree.createSceneObjectTreeItem(ESHumanPoi)
        treeItem.sceneObject.position = position
        treeItem.sceneObject.name = name
        treeItem.sceneObject.poiMode = "CircularV01"
        treeItem.sceneObject.show = this.show
        treeItem.sceneObject.flyToParam = {
            "distance": 24.853199747471248,
            "heading": 242.72284805652384,
            "pitch": -30.344736492499575,
            "flyDuration": 1,
            "hDelta": 0.20846023778847228,
            "pDelta": 2.927719620309951
        }
        this._map.set(id, treeItem)
    }

    update(item) {
        const treeItem = this._map.get(item.id)
        if (treeItem) {
            treeItem.sceneObject.smoothMove(item.position, 15)
            treeItem.sceneObject.animation = 'walking'
        }
    }
}